package com.kfm.springmybatis.Collection;

import com.kfm.springmybatis.model.Student;
import com.kfm.springmybatis.service.StudentService;
import com.kfm.springmybatis.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class StudentCollection {
    @Autowired
    StudentService studentService;

    @RequestMapping("/listPage")
    public ModelAndView selectAll() {
        ModelAndView mv = new ModelAndView();
        List<Student> students = studentService.queryStudent();
        mv.addObject(Constants.STUDENT_LIST, students);
        mv.setViewName("student");
        return mv;
    }

    @RequestMapping("/add")
    public String addUser(
            @RequestParam("name") String name,
            @RequestParam("gender") String gender,
            @RequestParam("age") String age,
            @RequestParam("score") double score,
            RedirectAttributes attributes
    ) {
        int integer = Integer.parseInt(age);
        Student student = new Student(name, gender, integer, score);
        int i = studentService.addStudent(student);
        if (i > 0) {
            attributes.addFlashAttribute(Constants.STUDENT_LIST_KEY, "添加成功");
            return "redirect:/listPage";
        } else {
            attributes.addFlashAttribute(Constants.STUDENT_LIST_KEY, "添加失败");
            return "redirect:/listPage";
        }
    }

    @RequestMapping("/delete")
    public String deleteUser(
            @RequestParam("id") int id,
            RedirectAttributes attributes) {
        int i = studentService.deleteStudent(id);
        if (i != 0) {
            attributes.addFlashAttribute(Constants.STUDENT_LIST_KEY, "删除成功");
            return "redirect:/listPage";
        } else {
            attributes.addFlashAttribute(Constants.STUDENT_LIST_KEY, "删除失败");
            return "redirect:/listPage";
        }
    }

    @RequestMapping("/updateOne")
    public String updateUser(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("gender") String gender,
            @RequestParam("age") Integer age,
            @RequestParam("score") double score,
            RedirectAttributes attributes
    ) {
        //将age转换为int类型
        Student student = new Student(id, name, gender, age, score);
        int i = studentService.updateStudent(student);
        if (i > 0) {
            attributes.addFlashAttribute(Constants.STUDENT_LIST_KEY, "修改成功");
            return "redirect:/listPage";
        } else {
            attributes.addFlashAttribute(Constants.STUDENT_LIST_KEY, "修改失败");
            return "redirect:/listPage";
        }
    }

    /*
    分页查询
     */
    @RequestMapping("/listPageByPage")
    public ModelAndView selectAllByPage(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "5") int size) {
        List<Student> students = studentService.queryStudentByPage(page, size);
        return new ModelAndView("student", Constants.STUDENT_LIST, students);

    }
}
